﻿using MediatR;
using Swork03_28.Api.Application.Commands.FarmedCommand;
using Swork03_28.Api.Application.Commands.TraceableCommand;
using Swork03_28.Services.Interface;

namespace Swork03_28.Api.Application.CommandHandlers.TraceableHandler
{
    public class VaccineDelImgCommandHandler : IRequestHandler<TraceableDelImgCommand, int>
    {
        private readonly ITraceableService traceableService;

        public VaccineDelImgCommandHandler(ITraceableService traceableService)
        {
            this.traceableService = traceableService;
        }

        public async Task<int> Handle(TraceableDelImgCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            var info = await traceableService.GetAsync(request.Id);
            var Imgs = info.TestImgs.Split(",").ToList();
            Imgs.RemoveAll(x => x == request.Img);
            info.TestImgs = string.Join(",", Imgs);
            count += await traceableService.UpdateAsync(info);
            return count;
        }
    }
}
